import json
import os
import re
import unittest

import ddt

from cn.boz.utils.ProjectUtils import ProjectUtils
from cn.boz.utils.UploadUtils import UploadUtils
from cn.boz.utils.datatheif.dataTheif import DataTheif


@ddt.ddt
class TestCase(unittest.TestCase):

    def test01(self):
        print(ProjectUtils.getProjectPath())
        print(ProjectUtils.getResourcePath())
        pass

    def test_no2(self):
        val = ProjectUtils.getConfigSessionValue('test', 'username')
        print(val)

    def test03(self):
        dt = DataTheif()
        dt.theifForProductList(1, 24, True)

    def test04(self):
        """
        转化商品到实际可以使用的商品
        """
        fpath = os.path.join(ProjectUtils.getProjectPath(), '/Volumes/Mac/Code/pycharm/MyAI/temp/merchant/product/')
        fs = os.listdir(fpath)
        # fs = list(filter(lambda f: 'json' in f, fs))
        destPath='%s/convert'%(fpath)
        if not os.path.isdir(destPath):
            os.makedirs(destPath)
        for f in fs:
            with open(os.path.join(fpath, f), 'r+') as fp:
                with open(os.path.join(destPath, '%s.json'%(f)), 'w+') as fop:
                    obj = json.load(fp)
                    data = obj['data']
                    data['_id'] = data['id']
                    del data['id']
                    data['price'] = 0
                    json.dump(data, ensure_ascii=False, fp=fop)
        for f in os.listdir(destPath):
            with open(os.path.join(destPath, f), 'r+') as fp:
                text=fp.read()
                fp.seek(0)
                fp.truncate()
                text=text.replace('https://aidi-public.oss-cn-zhangjiakou.aliyuncs.com','',-1)
                fp.write(text)
                print(text)



    def test05(self):
        """
        偷取商品，按照给定的ID进行偷取
        """
        dt = DataTheif()
        items = ['10350', '10349', '10348', '10347', '10346', '10345', '10344', '10343', '10342']
        for item in items:
            dt.theifForProductById(item)

    def test06(self):
        """上传图片"""
        uploadUtils = UploadUtils()
        uploadUtils.uploadDirectory("/Volumes/Mac/Code/pycharm/MyAI/temp/20210110/commodity", "commodity/")

    # def test07(self):
    #     app=QApplication()
    #     window=QMainWindow()
    #     btn=QPushButton(window)
    #     btn.setText("Hello")
    #     btn.clicked.connect(lambda x:print(x))
    #     window.show()
    #     app.exec_()



if __name__ == "__main__":
    unittest.main()
